<?php
namespace app\index\controller;
use AlipayService;
use WxpayService;
use think\Config;
use think\Db;

class Pay
{
    public function aliPay()
    {
        require EXTEND_PATH.'pay/alipay.php';
        $appid = config('pay.ali_appid');  //https://open.alipay.com 账户中心->密钥管理->开放平台密钥，填写添加了电脑网站支付的应用的APPID
        $notifyUrl = config('pay.ali_notifyUrl');     //付款成功后的异步回调地址
        $outTradeNo = $_REQUEST['out_trade_no']; //你自己的商品订单号'test123456789'
        $payAmount = $_REQUEST['total_amount'];//付款金额，单位:元
        $orderName = $_REQUEST['subject']; //订单标题
        $source    = $_REQUEST['source'];//订单来源
        $sourceList=config('source');
        if(empty($outTradeNo)||empty($orderName)||empty($payAmount)||!isset($sourceList[$source])){
            echo "参数错误";exit();
        }
        //WAIT_BUYER_PAY
        $pay_re = Db::table('ims_zh_pay_order')->where(array('out_trade_no'=>$outTradeNo))->find();//查询订单记录 1232113233
        if($pay_re){
            echo "无效订单";exit();
        }
        $signType = 'RSA2';       //签名算法类型，支持RSA2和RSA，推荐使用RSA2
//商户私钥，填写对应签名算法类型的私钥，如何生成密钥参考：https://docs.open.alipay.com/291/105971和https://docs.open.alipay.com/200/105310
        $saPrivateKey=config('pay.ali_key');
        $aliPay = new AlipayService($appid,'',$notifyUrl,$saPrivateKey);
        $result = $aliPay->doPay($payAmount,$outTradeNo,$orderName,'',$notifyUrl);
        $result = $result['alipay_trade_precreate_response'];
        if($result['code'] && $result['code']=='10000'){
            $data=[
                'out_trade_no' => $outTradeNo,
                'total_amount' => $payAmount,
                'subject' =>$orderName,
                'alipay_app_id'=> $appid,//上线后需要改
                'alipay_seller_id'=>'2088421285967882',//上线后需要改
                'add_time'=>date('Y-m-d H;i:s'),
                'update_time'=>date('Y-m-d H;i:s'),
                'alipay_trade_status'=>'WAIT_BUYER_PAY',
                'pay_way'=>1,//1-阿里支付，2-微信支付
                'source' => $source,
            ];
            //记录支付数据
            Db::table('ims_zh_pay_order')->insert($data);
            echo  $this->qrcode($result['qr_code']);
            exit;
        }else{
            echo $result['msg'].' : '.$result['sub_msg'];
        }
    }

    public function wxPay()
    {
        require EXTEND_PATH.'pay/wxpay.php';
        $mchid = config('pay.wx_mchid');          //微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送
        $appid = config('pay.wx_appid');   //公众号APPID 通过微信支付商户资料审核后邮件发送
        $apiKey = config('pay.wx_apiKey');   //https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥
        $source    = $_REQUEST['source'];//订单来源
        $sourceList=config('source');
        $wxPay = new WxpayService($mchid,$appid,$apiKey);
        $notifyUrl = config('pay.wx_notifyUrl');     //付款成功后的异步回调地址
        $outTradeNo = $_REQUEST['out_trade_no'];//$_REQUEST['out_trade_no'];     //你自己的商品订单号'test123456789'
        $payAmount = $_REQUEST['total_amount'];//$_REQUEST['payAmount'];          //付款金额，单位:元
        $orderName = $_REQUEST['subject'];//$_REQUEST['orderName'];    //订单标题
        if(empty($outTradeNo)||empty($orderName)||empty($payAmount)||!isset($sourceList[$source])){
            echo "参数错误";exit();
        }
        $payTime = time();      //付款时间
        $arr = $wxPay->createJsBizPackage($payAmount,$outTradeNo,$orderName,$notifyUrl,$payTime);
        if(!empty($arr['code_url'])){
            $data = [
                'out_trade_no' => $outTradeNo,
                'total_amount' => $payAmount, // **单位：分**
                'wechat_app_id' =>  $appid,//上线后需要改
                'wechat_mch_id' =>  $mchid,//上线后需要改
                'wechat_trade_status' => '',
                'pay_way' => 2,//1-阿里支付，2-微信支付
                'subject' => $orderName,
                'add_time' => date('Y-m-d H;i:s'),
                'update_time' => date('Y-m-d H;i:s'),
                'source' => $source,
            ];
            //记录支付数据
            Db::table('ims_zh_pay_order')->insert($data);
        }
        //生成二维码
        $this->qrcode($arr['code_url']);
        exit();
    }


    public function qrcode($url="",$level=3,$size=4)
    {
        Vendor('phpqrcode.phpqrcode');
        $errorCorrectionLevel =intval($level) ;//容错级别
        $matrixPointSize = intval($size);//生成图片大小
        //生成二维码图片
        $object = new \QRcode();
        var_dump($object);die;
        $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
    }

}
